home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / computerjanitorapp / ui_cli.py < prev    next >
Encoding:
Python Source  |  2009-03-04  |  5.2 KB  |  174 lines

  1. # ui_cli.py - command line user interface
  2. # Copyright (C) 2008, 2009  Canonical, Ltd.
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, version 3 of the License.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  15.  
  16.  
  17. import os
  18. import logging
  19. import sys
  20. import textwrap
  21. import traceback
  22.  
  23. import computerjanitor
  24. import computerjanitorapp
  25. _ = computerjanitorapp.setup_gettext()
  26.  
  27.  
  28. class UnknownCommand(computerjanitor.Exception):
  29.  
  30.     def __init__(self, name):
  31.         self._str = _("Unknown command: %s") % name
  32.  
  33.  
  34. class UnknownCruft(computerjanitor.Exception):
  35.  
  36.     def __init__(self, name):
  37.         self._str = _("Unknown cruft: %s") % name
  38.  
  39.  
  40. class MustBeRoot(computerjanitor.Exception):
  41.  
  42.     def __init__(self):
  43.         self._str = _("computer-janitor must be run as root, sorry.")
  44.  
  45.  
  46. class CommandLineUserInterface(computerjanitorapp.UserInterface):
  47.  
  48.     def run(self, options, args):
  49.         if self.mustberoot and os.getuid() != 0:
  50.             raise MustBeRoot()
  51.  
  52.         self.app.verify_apt_cache()
  53.     
  54.         dict = {
  55.             "find": self.show_cruft,
  56.             "cleanup": self.cleanup,
  57.             "ignore": self.ignore,
  58.             "unignore": self.unignore,
  59.             "help": self.help,
  60.         }
  61.  
  62.         if args:
  63.             cmd = args[0]
  64.             args = args[1:]
  65.         else:
  66.             cmd = "help"
  67.             args = []
  68.         
  69.         if cmd in dict:
  70.             app = self.app
  71.             app.state.load(options.state_file)
  72.             try:
  73.                 dict[cmd](options, args)
  74.             except Exception, e: # pragma: no cover
  75.                 logging.debug(unicode(traceback.format_exc()))
  76.                 logging.critical(unicode(e))
  77.                 sys.exit(1)
  78.         else:
  79.             raise UnknownCommand(cmd)
  80.     
  81.     def find_cruft(self):
  82.         list = []
  83.         for plugin in self.pm.get_plugins():
  84.             for cruft in plugin.get_cruft():
  85.                 list.append(cruft)
  86.         return self.app.remove_whitelisted(list)
  87.     
  88.     def show_one_cruft(self, name, desc, s, width): #pragma: no cover
  89.         if width is None:
  90.             max = len(name) + len(desc)
  91.         else:
  92.             max = width
  93.             max -= 9 # state column
  94.             max -= 2 # two spaces
  95.             max -= 1 # avoid the last column, some terminals word wrap there
  96.  
  97.         print "%-9s  %.*s " % (s, max, name)
  98.         if desc:
  99.             paras = desc.split("\n\n")
  100.             for para in paras:
  101.                 for line in textwrap.wrap(para, max):
  102.                     print "%9s  %s" % ("", line)
  103.                 print
  104.     
  105.     def show_cruft(self, options, args):
  106.         list = []
  107.         maxname = ""
  108.         state = self.app.state
  109.         for cruft in self.find_cruft():
  110.             name = cruft.get_name()
  111.             if options.verbose:
  112.                 desc = cruft.get_description() # pragma: no cover
  113.             else:
  114.                 desc = None
  115.             if state.is_enabled(name):
  116.                 s = _("removable")
  117.             else:
  118.                 s = _("ignored")
  119.             list.append((name, desc, s))
  120.             if len(name) > len(maxname):
  121.                 maxname = name
  122.  
  123.         rows, cols = computerjanitorapp.get_terminal_size()
  124.         for name, desc, s in sorted(list):
  125.             self.show_one_cruft(name, desc, s, cols)
  126.  
  127.     def cleanup(self, options, args):
  128.         crufts = {}
  129.         for cruft in self.find_cruft():
  130.             crufts[cruft.get_name()] = cruft
  131.         
  132.         if args:
  133.             for arg in args:
  134.                 if arg not in crufts:
  135.                     raise UnknownCruft(arg)
  136.         elif options.all:
  137.             state = self.app.state
  138.             args = []
  139.             for name in crufts.keys():
  140.                 if state.is_enabled(name):
  141.                     args.append(name)
  142.                 else:
  143.                     logging.info(_("Ignored: %s") % name)
  144.     
  145.         for arg in args:
  146.             if options.no_act:
  147.                 logging.info(_("Pretending to remove cruft: %s") % arg)
  148.             else:
  149.                 logging.info(_("Removing cruft: %s") % arg)
  150.                 crufts[arg].cleanup()
  151.         for plugin in self.pm.get_plugins():
  152.             if options.no_act:
  153.                 logging.info(_("Pretending to post-cleanup: %s") % plugin)
  154.             else:
  155.                 logging.info(_("Post-cleanup: %s") % plugin)
  156.                 plugin.post_cleanup()
  157.  
  158.     def ignore(self, options, cruft_names):
  159.         state = self.app.state
  160.         for cruft_name in cruft_names:
  161.             state.disable(cruft_name)
  162.         if not options.no_act:
  163.             state.save(options.state_file)
  164.  
  165.     def unignore(self, options, cruft_names):
  166.         state = self.app.state
  167.         for cruft_name in cruft_names:
  168.             state.enable(cruft_name)
  169.         if not options.no_act:
  170.             state.save(options.state_file)
  171.  
  172.     def help(self, options, args):
  173.         self.app.parser.print_help()
  174.